import http from "@/utils/http";
import { encrypt } from "@/utils/";
import { FORM_DATA_AXIOS_CONFIG } from "../constants";

/**
 * 列表查询
 * @param {number} page
 * @param {number} size
 * @param {string} keyword
 */
export function getUsersData(page: number, size: number, keyword: string) {
  const url = `/User/selfdepiduser/?page=${page}&page_size=${size}&search=${keyword}`;
  return http.get(url);
}

/**
 * 获得用户详情
 * @param {string} id
 * @returns
 */
export function getUserDetail(id: string) {
  const url = `/User/${id}/showtheuser/`;
  return http.get(url);
}

/**
 * 查出所有部门
 */
export function getUserDeps() {
  const url = "/Department/showuserdepid/";
  return http.get(url);
}

/**
 * 更新用户信息
 * @param {*} form
 */
export function updateUser(form) {
  const password = form.password != "" ? encrypt(form.password) : "";

  const params = new FormData();
  params.append("username", form.username);
  params.append("depid", form.depid.id);
  params.append("puser", "ZJXCA0000");
  params.append("mobile", form.mobile);
  params.append("wxcode", form.wxcode);
  params.append("QQ", form.QQ);
  params.append("password", password);
  params.append("remark", form.remark);
  params.append("cnname", form.cnname);
  params.append("llevel", form.llevel);

  const url = `/User/${form.id}/updateuser/`;
  return http.post(url, params, FORM_DATA_AXIOS_CONFIG);
}

/**
 * 新增用户
 * @param {*} form
 */
export function insertUser(form) {
  const password = form.password != "" ? encrypt(form.password) : "";

  const params = new FormData();
  params.append("username", form.username);
  params.append("depid", form.depid.id);
  params.append("puser", "ZJXCA0000");
  params.append("mobile", form.mobile);
  params.append("wxcode", form.wxcode);
  params.append("QQ", form.QQ);
  params.append("password", password);
  params.append("remark", form.remark);
  params.append("cnname", form.cnname);
  params.append("llevel", form.llevel);

  const url = "/User/createuser/";
  return http.post(url, params, FORM_DATA_AXIOS_CONFIG);
}

/**
 * 物理删除人员
 * @param {string} id
 */
export function deleteUser(id: string) {
  const url = `/User/${id}/deleteuser/`;
  return http.get(url);
}

/**
 * 获得用户的所有角色
 * @param {string} id
 * @returns
 */
export function getUserRoles(id: string) {
  const url = `/User/${id}/showroledetail/`;
  return http.get(url);
}

/**
 * 获得用户所有所在组
 * @param {string} id
 */
export function getUserGroup(id: string) {
  const url = `/Emgroup/showallemgroup/`;
  const params = { user: id };
  return http.post(url, params);
}

/**
 * 获得平台列表
 */
export function getPlatList() {
  const url = "/Equipment/listplatclass/";
  return http.get(url);
}

/**
 * 创建用户平台关系
 */
export function createPlatUser(uid: string, pid: string) {
  const url = "/User/createuserplatform/";
  const form = new FormData();
  form.append("user_id", uid);
  form.append("alarmobjtype_id", pid);

  return http.post(url, form, FORM_DATA_AXIOS_CONFIG);
}

/**
 * 解锁用户
 * @param {string} id
 */
export function unlockUser(id: string) {
  const url = `/User/${id}/unlockuser/`;
  return http.get(url);
}
